home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / folder watching / folder watcher fba / fbaappleevents.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  6.5 KB  |  255 lines

  1. /*
  2.     File:        FBAAppleEvents.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Greg Sutton    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/21/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24.  
  25. #include "FBAAppleEvents.h"
  26.  
  27. #include "FBA.h"
  28. #include "FBALists.h"
  29.  
  30. #include <Events.h>
  31. #include <Errors.h>
  32. #include <AERegistry.h>
  33. #include <Gestalt.h>
  34. #include <Resources.h>
  35.  
  36. // prototypes
  37. static pascal OSErr HandleOpenApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  38. static pascal OSErr HandleQuitApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  39. static pascal OSErr HandleOpenDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  40. static pascal OSErr HandlePrintDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon );
  41.  
  42. static OSErr        HandleOpenDesc( AEDesc* theDesc );
  43. static OSErr        GetDescriptorData( const AEDesc* theDesc, Ptr destPtr, Size maxSize );
  44.  
  45. static OSErr        GetTargetAppDesc( OSType theAppCreator, AEDesc* theTarget );
  46.  
  47.  
  48. // Globals
  49. extern    short        gQuit;
  50.  
  51.  
  52. OSErr InitAppleEvents ( void )
  53. {
  54.     short err;
  55.     long response;
  56.     
  57.     err = Gestalt( gestaltAppleEventsAttr, &response );
  58.     if (noErr != err)
  59.         response = 0L;
  60.  
  61.     if ( ! ( response & (1 << gestaltAppleEventsPresent)) )
  62.         return gestaltUndefSelectorErr;
  63.     
  64.     err = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication,
  65.                         NewAEEventHandlerProc(HandleOpenApplication), 0L, false );
  66.     if (noErr != err) goto done;
  67.  
  68.     err = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication,
  69.                         NewAEEventHandlerProc(HandleQuitApplication), 0L, false );
  70.     if (noErr != err) goto done;
  71.  
  72.     err = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments,
  73.                         NewAEEventHandlerProc(HandleOpenDocuments), 0L, false );
  74.     if (noErr != err) goto done;
  75.  
  76.     err = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments,
  77.                         NewAEEventHandlerProc(HandlePrintDocuments), 0L, false );
  78.  
  79. done:        
  80.     return err;
  81. }
  82.  
  83.  
  84.  
  85. OSErr DoAppleEvent( EventRecord *event )
  86. {
  87.     short err = noErr;
  88.     
  89.     err = AEProcessAppleEvent ( event );
  90.     return err;
  91. }
  92.  
  93.  
  94.  
  95. static pascal OSErr HandleOpenApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  96. {
  97. #ifdef __MWERKS__
  98.     #pragma unused (theAppleEvent, theReply, theRefcon )
  99. #endif
  100.     // don't do anything here
  101.     return noErr;
  102. }
  103.  
  104.  
  105.  
  106. static pascal OSErr HandleQuitApplication( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  107. {
  108. #ifdef __MWERKS__
  109.     #pragma unused (theAppleEvent, theReply, theRefcon )
  110. #endif
  111.  
  112.     gQuit = true;
  113.     return noErr;
  114. }
  115.  
  116.  
  117. // If we get a folder dropped onto the application then we'll add it to our
  118. // list of folders to watch.
  119.  
  120. static pascal OSErr HandleOpenDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  121. {
  122. #ifdef __MWERKS__
  123.     #pragma unused( theReply, theRefcon )
  124. #endif
  125.  
  126.     AEDesc    directObj = { typeNull, NULL };
  127.     OSErr    err;
  128.  
  129.     err = AEGetParamDesc( theAppleEvent, keyDirectObject, typeWildCard, &directObj );
  130.     if ( noErr != err ) goto done;
  131.  
  132.     err = HandleOpenDesc( &directObj );
  133.  
  134. done:
  135.     AEDisposeDesc( &directObj );
  136.  
  137.     return err;
  138. }
  139.  
  140.  
  141. static OSErr    HandleOpenDesc( AEDesc* theDesc )
  142. {
  143.     AEDesc        aDesc = { typeNull, NULL };
  144.     FSSpec        anFSSpec;
  145.     long        aCount;
  146.     DescType    anAEKeyword;
  147.     OSErr        err;
  148.  
  149.     switch ( theDesc->descriptorType )
  150.     {
  151.         case typeAEList:
  152.             err = AECountItems( theDesc, &aCount );
  153.             if ( noErr != err ) goto done;
  154.             
  155.             for ( ; aCount > 0; aCount-- )
  156.             {
  157.                 err = AEGetNthDesc( theDesc, aCount, typeWildCard, &anAEKeyword, &aDesc );
  158.                 if ( noErr != err ) goto done;
  159.                 
  160.                 err = HandleOpenDesc( &aDesc );
  161.                 if ( noErr != err ) goto done;
  162.                 
  163.                 AEDisposeDesc( &aDesc );
  164.             }
  165.             break;
  166.         
  167.         default:
  168.             err = AECoerceDesc( theDesc, typeFSS, &aDesc );
  169.             if ( noErr != err ) goto done;
  170.             
  171.             err = GetDescriptorData( &aDesc, (Ptr)&anFSSpec, sizeof( anFSSpec ) );
  172.             if ( noErr != err ) goto done;
  173.             
  174.             if ( ! FolderInList( &anFSSpec ) )
  175.                 if ( AddFolder( &anFSSpec ) )            // If this is a new folder to watch
  176.                     AddFolderPathResource( &anFSSpec );    // then add it to our resources.
  177.     }
  178.  
  179. done:
  180.     AEDisposeDesc( &aDesc );
  181.     
  182.     return err;
  183. }
  184.  
  185.  
  186. static OSErr    GetDescriptorData( const AEDesc* theDesc, Ptr destPtr, Size maxSize )
  187. {
  188.     Size    copySize;
  189.     OSErr    err;
  190.     
  191.     if ( typeNull == theDesc->descriptorType || ! theDesc->dataHandle )
  192.         return( errAENotAEDesc );
  193.  
  194.     copySize = GetHandleSize( (Handle)theDesc->dataHandle );
  195.     if ( copySize <= maxSize )
  196.     {
  197.         HLock( (Handle)theDesc->dataHandle );
  198.         BlockMoveData( *theDesc->dataHandle, destPtr, copySize );
  199.         HUnlock( (Handle)theDesc->dataHandle );
  200.     }
  201.     else
  202.         err = errAECorruptData;
  203.     
  204.  
  205.     return noErr;
  206. } // GetDescriptorData
  207.  
  208.  
  209.  
  210. static pascal OSErr HandlePrintDocuments( AppleEvent *theAppleEvent, AppleEvent *theReply, long theRefcon )
  211. {
  212. #ifdef __MWERKS__
  213.     #pragma unused(theAppleEvent, theReply, theRefcon )
  214. #endif
  215.  
  216.     // Faceless Background Applications can't print documents
  217.     return errAEEventNotHandled;
  218. }
  219.  
  220.  
  221. OSErr    SendChangeEvent( ProcessSerialNumber* thePSN, FSSpec* theSpec, DescType theChangeType )
  222. {
  223.     AppleEvent         anAppleEvent = {typeNull,NULL},
  224.                     aReply = {typeNull,NULL};
  225.     AEDesc           targetDesc = {typeNull,NULL},
  226.                     specDesc = {typeNull,NULL};
  227.     OSErr           err;
  228.     
  229.     err = AECreateDesc( typeProcessSerialNumber, (Ptr)thePSN, 
  230.                                      sizeof( ProcessSerialNumber ), &targetDesc );
  231.     if ( noErr != err ) goto done;
  232.  
  233.         // Create the AppleEvent
  234.     err = AECreateAppleEvent( kFolderWatcherSuite, theChangeType, &targetDesc,
  235.                                 kAutoGenerateReturnID, kAnyTransactionID, &anAppleEvent );
  236.     if ( noErr != err ) goto done;
  237.     
  238.     err = AECreateDesc( typeFSS, (Ptr)theSpec, sizeof( FSSpec ), &specDesc );
  239.     if ( noErr != err ) goto done;
  240.    
  241.     err = AEPutParamDesc(&anAppleEvent, keyDirectObject, &specDesc );
  242.     if ( noErr != err ) goto done;
  243.          
  244.     err = AESend( &anAppleEvent, &aReply, kAENoReply, kAENormalPriority,
  245.                                                 kAEDefaultTimeout, NULL, NULL );
  246.  
  247. done:
  248.     AEDisposeDesc( &anAppleEvent );
  249.     AEDisposeDesc( &aReply );
  250.     AEDisposeDesc( &targetDesc );
  251.     AEDisposeDesc( &specDesc );
  252.         
  253.     return err;
  254. }
  255.